<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>模板</title>
  </head>
  <body>
    <div id="app">
      <h2>1.全选和反选</h2>
      <input type="checkbox" name="" id="" v-model="allcheck" /> 全选
      <ul>
        <li v-for="item in goods">
          <input type="checkbox" name="" id="" v-model="item.checked" /> {{
          item.name }}
        </li>
      </ul>
    </div>
  </body>
  <script src="../node_modules/vue/dist/vue.js"></script>
  <script>
    new Vue({
      el: "#app",
      data: {
        goods: [
          {
            name: "西瓜",
            checked: false,
          },
          {
            name: "哈密瓜",
            checked: false,
          },
          {
            name: "木瓜",
            checked: false,
          },
        ],
      },

      //计算属性
      computed: {
        allcheck: {
          get() {
            //getter方法
            return this.goods.every((item) => item.checked == true);
          },
          set(val) {
            //setter方法
            console.log(val, 888);
            this.goods.forEach((item) => {
              item.checked = val;
            });
          },
        },
      },
    });
  </script>
</html>
